﻿using System;
using HISModels.ViewModel;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Permissions;
using System.Security.Principal;
using System.Web.Security;
using System.Threading;
using KeanuMvc.Reference;
using HIS.Utils.Securities;
using System.IO;
using HIS.Utils;
using HISModels.Entities;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace HIS.Controllers.PatientData {
    public class MedicalHistoryPatient : HIS.Controllers.AppBaseController {

        #region Fields
        private string strAction = string.Empty;
        private string strNotification = string.Empty;
        string strUri = string.Empty;
        string strJson = string.Empty;
        private StringReader stringReader = null;

        #endregion
        public MedicalHistoryPatient() {
            try {

            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }


        [Authorization(RoleAction = "patient-medicalrecord")]
        public override void Index() {
            try {
            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);

            } finally {
                RenderFile("Patient\\medicationrecord.html");
            }
        }

        [Authorization(RoleAction = "patient-medicalrecord")]
        public void LoadTabHistories() {


            StringReader stringReader = null;
            try {

                
                strUri = Global.ServiceUriDictionary["medicalrecord"] + "169" + Global.ServiceUriDictionary["requestformat"];
                stringReader = base.RestService.GetJson(strUri);
                //medicalRecord = JsonConvert.DeserializeObject<HISModels.Entities.MedicalRecord>(stringReader.ReadToEnd());
              
                
            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            } finally {
                RenderFile("Patient\\acordionhistories.html");
                if (stringReader != null) stringReader.Dispose();
            }
        }

        [Authorization(RoleAction = "patient-medicalrecord")]
        public void TableListVitalSign() {


            try {
                string strJson = string.Empty;
                string sEcho = HttpContext.Current.Request["sEcho"];

                string ssearch = string.Empty;

                PostDataTablesNet pagingSearch = new PostDataTablesNet();
                pagingSearch.iDisplayLength = Convert.ToInt32(HttpContext.Current.Request["iDisplayLength"]);
                pagingSearch.iDisplayStart = Convert.ToInt32(HttpContext.Current.Request["iDisplayStart"]);
                pagingSearch.sEcho = Convert.ToInt32(HttpContext.Current.Request["sEcho"]);
                pagingSearch.sSearch = ssearch;

                string strUri = Global.ServiceUriDictionary["mrvitalsignpage"] + Global.ServiceUriDictionary["requestformat"];
                stringReader = base.RestService.PostJson(strUri, JsonConvert.SerializeObject(pagingSearch));
               
                JObject o = JObject.Parse(stringReader.ReadToEnd());
                JArray aaData = (JArray)o["aaData"];
                ///Modify it
                JArray aaDataModified = new JArray();
                for (int i = 0; i < aaData.Count(); i++) {
                    JObject oo = JObject.Parse(aaData[i].ToString());
                  
                    aaDataModified.Add(oo);

                }
                o["aaData"] = aaDataModified;

                ///So serialize
                strJson = JsonConvert.SerializeObject(o);
                HttpContext.Current.Response.Write(strJson);

            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            } finally {
                if (stringReader != null) stringReader.Dispose();
                base.RestService.Dispose();
            }
        }

    }


}